import * as constants from '../../../common/Constants';
import * as cacheUtils from '../../../model/CacheUtils';
const regexUtils = require('../../../utils/RegexUtils.js');
const UmfDialog = require('../../../utils/UmfDialog.js');
const network_utils = require('../../../http/NetworkUtils.js');
import TimeUtils from '../../../utils/TimeUtils';
import * as UmfUtils from '../../../utils/UmfUtils';
import { saveUserCache } from '../../../model/CacheUtils';

import {$Toptips, $Toast} from '../../../components/CompRouter';

let cacheManager = null;
let cacheDates = [];
const app = getApp();

Page({

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        cacheManager = new cacheUtils.cacheManager()
            .init('sendBillEmail', constants.STORAGE_FUNC.SENDBILLEMAIL);
        cacheDates = cacheManager.getFunc();
        cacheDates = cacheDates? cacheDates: [];
        let userCache = app.globalData.userCache;

        this.setData({
            cacheEmail: userCache["agentEmail"]? userCache["agentEmail"]: "",
            startDate: TimeUtils.getLastestYear(1, "yyyy-MM-dd"),
            endDate: TimeUtils.getLatestDate(1, "yyyy-MM-dd"),
        })
    },

    bindDateChange: function (e) {
        this.setData({
            selectDate: e.detail.value
        })
    },

    InfoSubmit: function (e) {
        let email = e.detail.value["linkMan"];
        let errMsg = this._checkForm(email);
        if (errMsg) {
            $Toptips.error({text: errMsg});
            return;
        }

        //校验通过，缓存代理商邮箱
        saveUserCache({agentEmail: email});

        if (cacheDates.indexOf(this.data.selectDate) < 0) {
            //没获取过 对账邮件
            this.sendBillEmailRequest(email);
        } else {
            //获取过 对账邮件
            UmfDialog.showAll("当天已获取过对账邮件,是否再次获取?", "确定", "取消", () => {}, () => {
                this.sendBillEmailRequest(email);
            });
        }
    },

    sendBillEmailRequest(email) {
        let params = {
            email: email,
            billDate: this.data.selectDate
        };
        network_utils.buildRequest(constants.Interfaces.FUNC_AGENT_SEND_BILLEMAIL, params, {
            doHttpSucess: (data)=> {
                if (constants.RetCodes.SUCCESS === data.retCode) {
                    //缓存最新的60条成功的日期
                    cacheDates = UmfUtils.getRecentList(cacheDates, this.data.selectDate, 60);
                    cacheManager.cacheFunc(cacheDates);

                    $Toast.showText("对账邮件将在3 - 5分钟后送达，请注意查收");
                    setTimeout(()=> {
                        wx.navigateBack({delta: 1});
                    }, 2000);
                    return true;
                }
                return false;
            },
        }).send();
    },

    _checkForm: function (linkedEmail) {
        let resultErrorMsg = regexUtils.regexTypeWithMsg(linkedEmail, constants.RegexTypes.LINKTELEMAIL);
        if (resultErrorMsg) {
            return resultErrorMsg;
        }

        if (!this.data.selectDate) {
            return "请选择账单日期";
        } else if (TimeUtils.isLaterThen(this.data.selectDate, this.data.endDate)
            || TimeUtils.isLaterThen(this.data.startDate, this.data.selectDate)) {
            return "请选择最近一年的账单日期";
        }
        return "";
    }
});

